/*===================================================================
het.do
	*Author(s):	David Phillips, Sean McConville
	*Purpose:	Uses processed data to make final tables and figures
				for heterogeneity analysis.
===================================================================*/

	clear
	use "$datadir/regdata.dta"
	cd "$outputdir"
	
	global subgroups  assmtscorez_med agez_med   famtimeYm malez hispanic black white currentbalancz_med engtroubleYz overcrowdYz unfithousYz owemoneyYz poorcreditYz disabledYz chronicheaYz mental_heaYz   violenceYz  priorengagementbinary  yhat_med
	*global pred c.assmtscorez c.agez malez hispanic black white  famtimeYm c.currentbalancz  unfithousYz owemoneyYz poorcreditYz chronicheaYz   violenceYz   priorengagementbinary_hp  priorengagementbinary_nothp new_addpre_12 

		
***Early sample only
	keep if early == 1
		
		
***
*** Heterogeneity Tables
***

	gen const = 1
	gen dummy = .
	gen dummy2 = .
	
	reg dh_3m const
	eststo _filler
	
	foreach var of varlist assmtscorez agez prisonNz currentbalancz yhat {
		gen `var'_med = 0
		su `var' if eligible_other == 0, det
		replace `var'_med = 1 if `var' > r(p50)
	}
	
	label var prisonNz_med  "Any Incarcerated, 6 mos"
	label var agez_med "Above Median Age"
	label var assmtscorez_med "Above Median Score"
	label var currentbalancz_med "Any Assets"
	label var yhat_med "Above Median Predicted Risk"
	label var famtimeYm "No Children"
	
	*het in main outcomes

		esttab _filler _filler _filler _filler _filler using "Tables\het.tex", replace ///
		keep(const) drop(const) ///
		mtitles("Control" "Treatment" "Control" "Treatment" "Adj. RF Dif-in-Dif") ///
		nonotes noobs  nonum nogaps fragment nolines

		esttab _filler _filler _filler _filler _filler using "Tables\het.tex", append ///
		keep(const) drop(const) ///
		mtitles("Has Char" "Has Char" "Not" "Not" "") ///
		nonotes noobs  nonum nogaps fragment
		
		global treatvar treatment_sum
		
		
		foreach var of varlist $subgroups  { 

			replace dummy=const
			reg non_hp_6m dummy if $treatvar == 0 & `var' == 1 & eligible_other == 0, nocons robust
				eststo _reg1
			reg non_hp_6m dummy if $treatvar == 1 & `var' == 1 & eligible_other == 0, nocons robust
				eststo _reg2
			reg non_hp_6m dummy if $treatvar == 0 & `var' == 0 & eligible_other == 0, nocons robust
				eststo _reg3
			reg non_hp_6m dummy if $treatvar == 1 & `var' == 0 & eligible_other == 0, nocons robust
				eststo _reg4		
		
			replace dummy=$treatvar*`var'
			xi: reg non_hp_6m dummy `var' $treatvar i.strata i.strata*`var' if eligible_other == 0, robust
				eststo _reg5

			replace dummy = hp_3mo*`var'
			replace dummy2 = $treatvar*`var'
			xi: ivreg non_hp_6m  (hp_3mo dummy	 =  $treatvar dummy2)  `var' i.strata if eligible_other == 0, robust
				eststo _reg6
					
			esttab _reg1 _reg2 _reg3 _reg4 _reg5  using "Tables\het.tex", append ///
			keep(dummy) varlabels(dummy "`: var label `var' '") ///
			cells("b(fmt(%9.3f) pattern(1 1 1 1 0)) b(fmt(%9.3f) star pattern(0 0 0 0 1))") ///
			starlevel(* 0.10 ** 0.05 *** 0.01) ///
			collabels(none) nomtitles noobs ///
			nodepvars nonum nonotes nolines fragment
			
			esttab _reg1 _reg2 _reg3 _reg4 _reg5 using "Tables\het.tex", append ///
			keep(dummy) varlabels(dummy " ") ///
			cells(se(fmt(%9.3f) par pattern(0 0 0 0 1)) ) ///
			starlevel(* 0.10 ** 0.05 *** 0.01) ///
			collabels(none) nomtitles noobs ///
			nodepvars nonum nonotes nolines fragment
		}	
		
		esttab _reg1 _reg2 _reg3 _reg4 _reg5 using "Tables\het.tex", append ///
		nogaps keep (dummy) drop(dummy) nomtitles nonum b(a2) ///
		nonotes fragment noobs ///
		addnotes("Notes: Statistical significance shown at 1(***) 5(**) and 10(*) percent levels.")

	
	*coefplot figure
	
		gen all = 1
		gen pre2020 =  rct_dt < date("12/31/2019","MDY") 
		
		foreach var in all pre2020 $subgroups   {
			xi: reg non_hp_6m $treatvar i.strata if `var' == 1 & eligible_other == 0, robust
			est store `var'

		}
		
		local plotlist all
		foreach var in $subgroups   {
			local plotlist `plotlist' || `var'
		}
		display "`plotlist'"

		
		coefplot `plotlist'  ,  ///
				keep ($treatvar)  mcolor(white)  bycoefs xtitle("ITT Treatment Effect at 12 Months") levels(95 ) ///
				ciopts(lwidth(3 ..) lcolor(blue*.6 blue*.8 blue*1)) ///
				xline(0, lcolor(gray) lpattern(dash)) ///
				bylabels("All"  "Above med score" "Above med age" "No children" "Male" "Hispanic" "Black" "White" "Any assets" ///
 						"Trouble with English" "Housing overcrowded" "Homeless past 3 years" "Owe money" "Poor credit" ///
 						"Disabled" "Chonic health condition" "Mental health condition"  "Violence threat" ///
 						"Past HMIS" "Above med predicted risk") ///
				scheme(LEO)
						
		graph export "Figures\subgroups.png", as(png) replace
	